package com.iemes.controller.entryform.purLest;

import java.lang.reflect.Field; 
import java.text.SimpleDateFormat; 
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.omg.CORBA.DATA_CONVERSION;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.EntryFormMap;
import com.iemes.entity.PurchaseFromMap;
import com.iemes.entity.PurlestFormMap;
import com.iemes.entity.QualityFromMap;
import com.iemes.entity.SuppFilleFromMap;
import com.iemes.mapper.SuppfilleMapper;
import com.iemes.mapper.entry.EntryMapper;
import com.iemes.mapper.entry.PurLestMapper;
import com.iemes.mapper.material.PurchaseMapper;
import com.iemes.mapper.material.QualityMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;


@RequestMapping("/purLest/")
@Controller
public class PurLestController extends BaseController {

	@Inject
	private SuppfilleMapper suppMapper;
	@Inject
	private PurchaseMapper purMapper;
	@Inject
	private QualityMapper qualityMapper;
	@Inject
	private EntryMapper entryMapper;
	@Inject 
	private PurLestMapper purlestMapper;
	
	@RequestMapping("examinepurde")
	public String examineprus(Model model){
		List<EntryFormMap> entryFromMap=entryMapper.findByWhere(new EntryFormMap());
		model.addAttribute("entry", entryFromMap);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/entryform/pur_lest/purLestform_list";
	}
	
	@ResponseBody
	@RequestMapping("purLest_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		//定义当前月的总天数，比如30,31,28,29
        int maxCurrentMonthDay=0;
        EntryFormMap entryFromMap = getFormMap(EntryFormMap.class);
		PurlestFormMap purlestFormMap = getFormMap(PurlestFormMap.class);
		Date currentTime = new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat month=new SimpleDateFormat("yyyy-MM");
		//每月第一天
		Calendar  claendar1  =  Calendar.getInstance();
		claendar1.set(Calendar.DAY_OF_MONTH, 1);
		System.out.println("本月第一天: "+sdf.format(claendar1.getTime()));
		//每月最后一天
		Calendar calendar2=Calendar.getInstance();
		maxCurrentMonthDay=calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
		calendar2.set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
		
		Calendar cal = Calendar.getInstance();
		
		System.out.println("本月最后一天: "+sdf.format(calendar2.getTime()));
		String dateString = sdf.format(currentTime);
		System.out.println("今日日期："+dateString);
		String datemoth = sdf.format(currentTime);
		System.out.println("本月月份："+datemoth);
		//遍历数据
		List<Map<String, Object>> entryFormMaps=entryMapper.getAllName(entryFromMap);
		List<Map<String, Object>> entryFormMapp=entryMapper.getpurLest(entryFromMap);
		List<Map<String, Object>> purles=purlestMapper.getAllName(purlestFormMap);
		
		
		for (Map<String, Object> map : entryFormMapp) {
			int count=0,sum = 0,Lcount= 0,Lsum=1,i=0,p=0;
			double ctotal = 0.0,ftotal = 0.0,Ltolal=0.0,Lprice=0.0;
			for (Map<String, Object> maps : entryFormMaps) {
				Object materialNo = map.get("materialNo");
				Object materialNos = maps.get("materialNo");
				Object suppNumber = maps.get("suppName2");
				Object suppNumber1 = map.get("suppName2");
				Object productName = maps.get("productName");
				Object brand = maps.get("brand");
				Object unit = map.get("unit");
				String tranDate = map.get("tranDate").toString(); 
				tranDate=tranDate.substring(0,tranDate.lastIndexOf("-"));
				System.out.println(tranDate);
				
				if((materialNo.equals(materialNos)) && (suppNumber.equals(suppNumber1))){
					count =(int) maps.get("aogcount");
					ctotal = (double) maps.get("total");
					Lcount =(int) maps.get("empCount");
					Lprice = (double) maps.get("newprice");
					
					//计算每种物料的收入总数
					sum  += count;
					//计算每种物料的收入金额
					ftotal += ctotal;
					//计算领用总数量
					Lsum += Lcount;
					//计算领用总价
					Ltolal += Lprice*Lsum;
					++i;
					int year = claendar1.get(Calendar.YEAR);
					int months = claendar1.get(Calendar.MONTH) + 1; 
					int monthd = claendar1.get(Calendar.MONTH) - 1; 
					int currentInCount1 = 0;
					double currentInTotal1 = 0;
					int finalBalCount1=0;
					double finalBalTotal1 = 0;
					String dates = year+"-"+months;
					
					//本月最后一天
					 if((dateString.equals(calendar2.getTime())) && (tranDate.equals(monthd)) ){
						for (Map<String, Object> map2 : purles) {
							finalBalCount1= (int) map2.get("finalBalCount");
							finalBalTotal1= (double) map2.get("finalBalPrice");
							//末期发出数量
							purlestFormMap.put("finalBalCount", finalBalCount1);
							//末期发出金额
							purlestFormMap.put("finalBalTotal", finalBalTotal1);
							//本期发出数量
							purlestFormMap.put("currentOutCount", Lsum-finalBalCount1);
							//本期发出金额
							purlestFormMap.put("currentOutTotal", Ltolal-finalBalTotal1);
						}
					}
					 //本月更新数据
					 else if((dateString.equals(claendar1.getTime()))&& (!tranDate.equals(monthd))){	//月初时
						/*//初期结存数量
						purlestFormMap.put("initialCount", sum);
						//初期结存金额
						purlestFormMap.put("initialTotal", ftotal);
						//本期收入数量
						purlestFormMap.put("currentInCount", sum);
						//本期收入金额
						purlestFormMap.put("currentInTotal", ftotal);	
						//本期发出数量
						purlestFormMap.put("currentOutCount", Lsum);
						//本期发出金额
						purlestFormMap.put("currentOutTotal", Ltolal);
						//本期收入平均单价
						purlestFormMap.put("initialPrice", ftotal/i);
						//本期发出平均单价
						purlestFormMap.put("currentOutPrice", Ltolal/i);
						
						purlestFormMap.put("materialNo", materialNo);
						purlestFormMap.put("productName", productName);
						purlestFormMap.put("brand", brand);
						purlestFormMap.put("unit", unit);*/
						
						//初期结存数量
						purlestFormMap.put("initialCount", currentInCount1);
						//初期结存金额
						purlestFormMap.put("initialTotal", currentInTotal1);
						//本期收入数量
						purlestFormMap.put("currentInCount", sum-currentInCount1);
						//本期收入金额
						purlestFormMap.put("currentInTotal", ftotal-currentInTotal1);	
						//本期发出数量
						purlestFormMap.put("currentOutCount", finalBalCount1);
						//本期发出金额
						purlestFormMap.put("currentOutTotal", finalBalTotal1);
						//本期收入平均单价
						purlestFormMap.put("initialPrice", ftotal/i);
						//本期发出平均单价
						purlestFormMap.put("currentOutPrice", Ltolal/i);
						
						purlestFormMap.put("materialNo", materialNo);
						purlestFormMap.put("productName", productName);
						purlestFormMap.put("brand", brand);
						purlestFormMap.put("unit", unit);
						
						purlestMapper.addEntity(purlestFormMap);
						purlestFormMap.clear();
					}/*else{
						
						for (Map<String, Object> map2 : purles) {
							
							currentInCount1= (int) map2.get("currentInCount");
							finalBalTotal1= (double) map2.get("currentInTotal");
						
						//初期结存数量
						purlestFormMap.put("initialCount", currentInCount1);
						//初期结存金额
						purlestFormMap.put("initialTotal", currentInTotal1);
						//初期平均单价
						purlestFormMap.put("initialPrice", (currentInCount1*currentInTotal1)/i);
						//本期收入数量
						purlestFormMap.put("currentInCount", sum-currentInCount1);
						//本期收入金额
						purlestFormMap.put("currentInTotal", ftotal-currentInTotal1);	
						//本期发出数量
						purlestFormMap.put("currentOutCount", Lsum);
						//本期发出金额
						purlestFormMap.put("currentOutTotal", Ltolal);
						//本期收入平均单价
						purlestFormMap.put("currentInPrice", ((sum-currentInCount1)*(ftotal-currentInTotal1))/Lsum);
						//本期发出平均单价
						purlestFormMap.put("currentOutPrice", Ltolal/Lsum);
						
						purlestFormMap.put("materialNo", materialNo);
						purlestFormMap.put("productName", productName);
						purlestFormMap.put("brand", brand);
						purlestFormMap.put("unit", unit);
						
						purlestMapper.editEntity(purlestFormMap);
						purlestFormMap.clear();
					}*/
					//月首更新
					
					
					//数据存入
					/*每个月月首把数据存入*/
					/*if(dateString.equals(claendar1.getTime())){
						//初期结存数量
						purlestFormMap.put("initialCount", sum);
						//初期结存金额
						purlestFormMap.put("initialTotal", ftotal);
						//本期收入数量
						purlestFormMap.put("currentInCount", 0);
						//本期收入金额
						purlestFormMap.put("currentInTotal", 0);	
					//每月月末
					} else if (dateString.equals(calendar2.getTime())) {
						p =+p;
						//本期发出数量
						purlestFormMap.put("currentOutCount", Lsum);
						//本期发出金额
						purlestFormMap.put("currentOutTotal", Ltolal);
						purlestFormMap.put("ci", p);
					} else {
						//初期结存数量
						purlestFormMap.put("initialCount", sum);
						//初期结存金额
						purlestFormMap.put("initialTotal", ftotal);
						//本期收入数量
						purlestFormMap.put("currentInCount", sum);
						//本期收入金额
						purlestFormMap.put("currentInTotal", ftotal);	
						//本期发出数量
						purlestFormMap.put("currentOutCount", Lsum);
						//本期发出金额
						purlestFormMap.put("currentOutTotal", Ltolal);
						//本期收入平均单价
						purlestFormMap.put("initialPrice", ftotal/i);
						//本期发出平均单价
						purlestFormMap.put("currentOutPrice", Ltolal/i);
						
						purlestFormMap.put("materialNo", materialNo);
						purlestFormMap.put("productName", productName);
						purlestFormMap.put("brand", brand);
						purlestFormMap.put("unit", unit);
						
					}*/
				}
				//月初第一次新增第二个月数据
				/*if(dateString.equals(claendar1.getTime())){
					purlestMapper.addEntity(purlestFormMap);
					purlestFormMap.clear();
				}else{
					
				}*/
			}
			
		}
			List<Map<String, Object>> purlestFormMaps=purlestMapper.getAllName(purlestFormMap);
			LuogicPaging(pageNow, pageSize, purlestFormMaps);
			pageView.setRecords(purlestFormMaps);
			return pageView;
	}
	
	@RequestMapping("purlest_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			PurlestFormMap purlestCheckFormMap=baseMapper.findbyFrist("id", id, PurlestFormMap.class);
			model.addAttribute("before",purlestCheckFormMap);
		}
		return Common.BACKGROUND_PATH + "/entryform/pur_lest/purLestform_edit";
	}
	
	
	
	
	@ResponseBody
	@RequestMapping(value="purlist_editEntity")
	@SystemLog(module="物料管理",methods="采购单-修改表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		PurlestFormMap purlestCheckFormMap=getFormMap(PurlestFormMap.class);
		baseMapper.editEntity(purlestCheckFormMap);
		return "success";
	}
	
	
	
}
